import hashlib


def getmd5(file):
    m = hashlib.md5()
    with open(file, 'rb') as f:
        for line in f:
            m.update(line)
    md5code = m.hexdigest()
    md5code = md5code.upper()
    return md5code


def getcrc(file, invert):
    with open(file, 'rb') as f:
        contents = f.read()
    a = 0xFFFF
    b = 0xA001
    for byte in contents:
        try:
            a ^= byte
        except Exception as e:
            raise e
        for i in range(8):
            last = a % 2
            a >>= 1
            if last == 1:
                a ^= b
    s = hex(a).upper()
    return s[4:6] + s[2:4] if invert == True else s[2:4] + s[4:6]


if __name__ == '__main__':
    a = getcrc("Protocol-TAC31-0810.bin", True)
